//
//  LXInsuranceUseApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/29.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXInsuranceUseApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXInsuranceUseModel.h"
#import "LXChooseBtnTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
#import "LXChooseManagerViewController.h"
#import "LXChooseCarViewViewController.h"
#import "FuZeRenModel.h"
#import "LXApplyAnnexTableCell.h"
#import "LXChooseCustomViewController.h"
#import "KeHuInfoModel.h"
#import "LXCooperationListViewController.h"
#import "LXCooperationModel.h"

@interface LXInsuranceUseApplyViewController()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;

@property (nonatomic) NSArray *pickCarData; // 提车数据源
@property (nonatomic) NSInteger pickState; // 提车状态0.未提车，1：已提车

@property (nonatomic) NSArray *repairStateData; // 维修数据源
@property (nonatomic) NSInteger repairState; // 维修状态0维修好   1已修好

@property (nonatomic) CheLiangModel *carModel; // 车辆数据
@property (nonatomic) NSString *chargeId; // 负责人id
@property (nonatomic) FuZeRenModel *fuZeModel; //  负责人模型


@property (nonatomic) NSMutableArray *annexAry;
@property (nonatomic) NSMutableArray *annexDataAry;

@property (nonatomic) KeHuInfoModel *kehuModel;
@property (nonatomic) NSString *customerId;
//修理厂id
@property (nonatomic) NSString *repairCompanyId;
//赔付状态 0处理中  1理赔完成
@property (nonatomic) NSInteger compensateState;


@end

@implementation LXInsuranceUseApplyViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.type==1) {
        [self layoutCustomNav:self.view title:@"新增出险记录"];
    }
    else {
        [self layoutCustomNav:self.view title:@"修改出险信息"];
    }
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXChooseBtnTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXChooseBtnTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    [self.tableView registerClass:[LXApplyAnnexTableCell class]
         forCellReuseIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];

    NSDictionary *dic = @{
        @"url":@"upLoad_B",
    };
    self.annexAry = [NSMutableArray array];
    self.annexDataAry = [NSMutableArray array];
    [self.annexAry addObject:dic];
    
    

    [self loadData];
    
    if (self.type==1) {
        [self setBottomViewWithText:@"确认新增"];
    }
    else {
        [self setBottomViewWithText:@"确认修改"];
    }
    

}

- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    // 默认未提车
    self.pickState = 0;
    self.compensateState = 0;
    [self.loanDic setValue:@"处理中" forKey:@"compensateState"];
    
    // 模型不为空,说明从车辆详情进入
    if (!ObjectIsEmpty(self.insuranceModel)) {

        // 车牌号
        [self.loanDic setValue:self.insuranceModel.carNum forKey:@"carNum"];
        self.carModel = [CheLiangModel new];
        self.carModel.ID = self.insuranceModel.carId;
        self.carModel.carNum = self.insuranceModel.carNum;
        
        // 修改的时候需要带入数据
        if (self.type==2) {

            // 负责人
            self.chargeId = self.insuranceModel.chargeId;
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.chargeName] forKey:@"chargeName"];
            // 客户
            self.customerId = self.insuranceModel.customerId;
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.customerName] forKey:@"customerName"];
            // 进厂日期
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.intoTime] forKey:@"intoTime"];
            // 出现日期
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.occurrenceTime] forKey:@"occurrenceTime"];
            // 出厂日期
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.outTime] forKey:@"outTime"];
            // 己方金额金额
            [self.loanDic setValue:StringIsEmpty(self.insuranceModel.selfAmount)?@"0":self.insuranceModel.selfAmount forKey:@"selfAmount"];
            // 第三方金额
            [self.loanDic setValue:self.insuranceModel.thirdAmount forKey:@"thirdAmount"];
            // 修理厂
            self.repairCompanyId = self.insuranceModel.repairCompanyId;
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.repairCompanyName] forKey:@"repairCompanyName"];
            // 责任方
            [self.loanDic setValue:[NSString stringWithNull:self.insuranceModel.chargeSide] forKey:@"chargeSide"];
            // 理赔状态
            self.compensateState = self.insuranceModel.compensateState;
            if (self.compensateState == 0) {
                [self.loanDic setValue:@"处理中" forKey:@"compensateState"];
            }
            else {
                [self.loanDic setValue:@"理赔完成" forKey:@"compensateState"];
            }
            // 提车状态
            self.pickState = self.insuranceModel.pickState;
            // 维修状态
            self.repairState = self.insuranceModel.repairState;
            
            // 备注/保养内容
            [self.loanDic setValue:self.insuranceModel.remark forKey:@"remark"];
            [self.loanDic setValue:StringIsEmpty(self.insuranceModel.descriptionStr)?@"无":self.insuranceModel.descriptionStr forKey:@"description"];


            //图片
            for (NSString *url in self.insuranceModel.pics) {
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,url];
                [self.annexAry insertObject:@{@"url":imageUrl} atIndex:0];
                [self.annexDataAry insertObject:url atIndex:0];
            }


        }

    }
    
    
   
    
    
    
    NSArray *ary0 = @[
        
        @{
            @"title":ObjectIsEmpty(self.insuranceModel)?@"*车牌号":@"车牌号",
            @"key":@"carNum",
            @"placeHolder" : @"请选择车牌号",
            @"canEdit":@(NO),
            @"showAccess":ObjectIsEmpty(self.insuranceModel)?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":ObjectIsEmpty(self.insuranceModel)?@"clickCarNum":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"负责人",
            @"key":@"chargeName",
            @"placeHolder" : @"请选择负责人",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickManager":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"租车客户",
            @"key":@"customerName",
            @"placeHolder" : @"请选择租车客户",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickCustomerName":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*出险日期":@"出险日期",
            @"key":@"occurrenceTime",
            @"placeHolder" : @"请选择出险日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickOccurrenceTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*进厂日期":@"进厂日期",
            @"key":@"intoTime",
            @"placeHolder" : @"请选择进厂日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickIntoTime":@"",
            @"height":@(kComHeight)
        },

        @{
            @"title":@"己方金额(元)",
            @"key":@"selfAmount",
            @"placeHolder" : @"请输入己方金额",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"inputType":@(NUM_DECIMAL),
            @"showAccess":@(NO),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*修理厂":@"修理厂",
            @"key":@"repairCompanyName",
            @"placeHolder" : @"请选择修理厂",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickCompanyAction":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"责任方",
            @"key":@"chargeSide",
            @"placeHolder" : @"请输入责任方",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"showAccess":@(NO),
            @"maxLength":@(1000),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"理赔状态",
            @"key":@"compensateState",
            @"placeHolder" : @"请选择理赔状态",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"maxLength":@(1000),
            @"sel":@"clickCompensateStateAction",
            @"height":@(kComHeight)
        },

        @{
            @"title":@"出厂日期",
            @"key":@"outTime",
            @"placeHolder" : @"请选择出厂日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickOutTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"出险附加图片(最多9张)",
            @"key":@"annex",
            @"height":@(kComAnnexHeight)
        },
        
    ];
    [self.data addObject:ary0];
    
    NSArray *ary1 = @[
        @{
            @"title":@"提车状态",
            @"key":@"pickState",
            @"height":@([LXChooseBtnTableViewCell height:self.pickCarData])
        },
        @{
            @"title":@"维修状态",
            @"key":@"repairState",
            @"height":@([LXChooseBtnTableViewCell height:self.pickCarData])
        },
        @{
            @"title":self.type==1?@"*第三方金额(元)":@"第三方金额(元)",
            @"key":@"thirdAmount",
            @"placeHolder" : @"请输入第三方金额",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"showAccess":@(NO),
            @"maxLength":@(7),
            @"sel":@"",
            @"height":@(kComHeight)
        },
      
        @{
            @"title":@"详细描述",
            @"key":@"description",
            @"placeHolder" : @"请输入...",
            @"height":@(150)
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"placeHolder" : @"请输入备注...",
            @"height":@(150)
        },
    
    ];
    
    [self.data addObject:ary1];

    
    
    
    
    
   
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        return [LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry] + 30;
    }
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        
        LXApplyAnnexTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLab.text = dic[@"title"];
        
        [cell reloadAnnexData:self.annexAry WithMaxNum:9];
        

        // 添加图片
        [cell setCallBackTakeAnnex:^(NSInteger num) {
            
            [self.view endEditing:YES];
            [[LXPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andMultipleImageBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andSelectedNum:9 - self.annexDataAry.count];
            
        }];
        
        
        // 放大图片
        [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
            [self.view endEditing:YES];
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:self.annexDataAry];
            [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
            
        }];
        // 删除图片
        [cell setCallBackDeleteAnnex:^(NSInteger num) {
            [self.view endEditing:YES];
            [self.annexAry removeObjectAtIndex:num];
            [self.annexDataAry removeObjectAtIndex:num];
            [self.tableView reloadData];
        }];
        

        return cell;
        
    }
    
    // 提车状态
    if ([dic[@"key"] isEqualToString:@"pickState"] ||
        [dic[@"key"] isEqualToString:@"repairState"]) {
       
        NSArray *tempAry;
        if ([dic[@"key"] isEqualToString:@"pickState"]) {
            tempAry = self.pickCarData;
        }
        else {
            tempAry = self.repairStateData;
        }
        
        

       LXChooseBtnTableViewCell *cell = [[LXChooseBtnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([LXChooseBtnTableViewCell class]) WithData:tempAry];

       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
        cell.canEdit = YES;
       cell.titleLab.text = dic[@"title"];
       [cell setClickBtnBlock:^(NSInteger Id) {
           if ([dic[@"key"] isEqualToString:@"pickState"]) {
               self.pickState = Id;
           }
           else {
               self.repairState = Id;
           }
           
       }];
       
       
       return cell;
    }
    // 保养内容
   else if([dic[@"key"]  isEqualToString:@"description"] ||
           [dic[@"key"]  isEqualToString:@"remark"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.maxLength = 200;
       cell.textView.placeholder = dic[@"placeHolder"];
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.loanDic[dic[@"key"]];
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.loanDic[dic[@"key"]]  = textStr;
       }];
       return cell;
   }
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    if(self.type == 2 && [dic[@"key"] isEqualToString:@"repairCompanyName"]){
        cell.textFiled.hidden = YES;
        cell.subTitleLab.hidden = NO;
        cell.subTitleLab.text = self.loanDic[dic[@"key"]];
    }
    
    
    
    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    cell.textfiledDidChangeBlock = ^(NSString * _Nonnull textStr) {
    
    };
    
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    vv.backgroundColor = ColorWithF7F8FA;
    return vv;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}

#pragma mark -- 选择进厂日期
-(void)clickIntoTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"intoTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择进厂日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        if([NSString compareDate:yearString withDate:self.loanDic[@"outTime"] withFormat:k_DateFormat] == -1){
            showAlert(@"进厂日期不能大于出厂日期");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
        }
    };
}
#pragma mark -- 选择出厂日期
-(void)clickOutTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"outTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择出厂日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        
        if([NSString compareDate:self.loanDic[@"intoTime"] withDate:yearString withFormat:k_DateFormat]==-1){
            showAlert(@"出厂日期不能小于进厂日期");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
        }
        
        
    };
}
#pragma mark -- 选择出险日期
-(void)clickOccurrenceTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"occurrenceTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择出险日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];

    };
}
//MARK: 理赔状态
-(void)clickCompensateStateAction{
    
    [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"处理中" twoBtnTitle:@"理赔完成" cancelHandlerBlock:^{
    } oneHandlerBlock:^{
        self.compensateState = 0;
        [self.loanDic setValue:@"处理中" forKey:@"compensateState"];
        [self.tableView reloadData];
    } twoHandlerBlock:^{
        self.compensateState = 1;
        [self.loanDic setValue:@"理赔完成" forKey:@"compensateState"];
        [self.tableView reloadData];
    } onVC:self];
    
}

#pragma mark -- 选择客户
-(void)clickCustomerName{
    LXChooseCustomViewController *vc = [[LXChooseCustomViewController alloc] init];
    [vc setToSendValue:^(KeHuInfoModel * _Nonnull model) {
        [self.loanDic setValue: [NSString stringWithFormat:@"%@",model.customerName]  forKey:@"customerName"];
        self.customerId = model.ID;
        self.kehuModel = model;
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}
#pragma mark -- 选择负责人
-(void)clickManager{
    [self.view endEditing:YES];
    
    LXChooseManagerViewController *vc = [[LXChooseManagerViewController alloc] init];
    WEAKSELF
    [vc setToSendValue:^(FuZeRenModel * _Nonnull model) {
        [weakSelf.loanDic setValue:model.realName forKey:@"chargeName"];
        weakSelf.chargeId = model.ID;
        weakSelf.fuZeModel = model;
        [weakSelf.tableView reloadData];
    }];
    vc.model = self.fuZeModel;
    [self pushToNextController:vc];
}



#pragma mark -- 选择车牌号
- (void)clickCarNum{
    
    LXChooseCarViewViewController *vc = [[LXChooseCarViewViewController alloc] init];
    vc.type = @"12";
    vc.comfrom = 1;
    [vc setToSendValue:^(CheLiangModel * _Nonnull model) {
        self.carModel = model;
        [self.loanDic setValue:model.carNum forKey:self.selDic[@"key"]];
        [self.tableView reloadData];
        
    }];
    [self pushToNextController:vc];
}
// MARK: 选择合作公司
-(void)clickCompanyAction{
    LXCooperationListViewController *vc = [[LXCooperationListViewController alloc] init];
    vc.type = 1;
    [vc setClickVcCallBlock:^(LXCooperationModel * _Nonnull model) {
        
        [self.loanDic setValue:model.name forKey:@"repairCompanyName"];
        self.repairCompanyId = model.ID;
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight - 5) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}



#pragma mark -- 提车状态数据源
- (NSArray *)pickCarData{
    _pickCarData = @[

        @{
            @"title":@"未提车",
            @"sel":@(NO),
            @"id":@(0)
        }.mutableCopy,
        @{
            @"title":@"已提车",
            @"sel":@(NO),
            @"id":@(1)
        }.mutableCopy,
    ];

    for (NSMutableDictionary *dic in _pickCarData) {
        if ([dic[@"id"] integerValue] == self.pickState) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }

    return _pickCarData;
}


#pragma mark -- 提车状态数据源
- (NSArray *)repairStateData{
    _repairStateData = @[

        @{
            @"title":@"维修中",
            @"sel":@(NO),
            @"id":@(0)
        }.mutableCopy,
        @{
            @"title":@"已修好",
            @"sel":@(NO),
            @"id":@(1)
        }.mutableCopy,
    ];

    for (NSMutableDictionary *dic in _repairStateData) {
        if ([dic[@"id"] integerValue] == self.repairState) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }

    return _repairStateData;
}

#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    
   
    if (StringIsEmpty(self.loanDic[@"carNum"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"occurrenceTime"])) {
        showAlert(@"请选择出险日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"intoTime"])) {
        showAlert(@"请选择进厂日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"repairCompanyName"])) {
        showAlert(@"请选择修理厂");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"thirdAmount"])) {
        showAlert(@"请输入第三方金额");
        return;
    }
   
    NSMutableDictionary *dic = @{
        
        @"carId":self.carModel.ID, // 所属车辆id
        @"carNum":self.carModel.carNum,// 所属车辆车牌号
        @"chargeName":self.loanDic[@"chargeName"],// 负责人
        @"chargeId":self.chargeId,// 负责人id
        @"customerName":self.loanDic[@"customerName"],//客户
        @"customerId":self.customerId,//客户id
        @"occurrenceTime":[NSString stringRemoveWithNull:self.loanDic[@"occurrenceTime"]],//出险日期
        @"intoTime":[NSString stringRemoveWithNull:self.loanDic[@"intoTime"]],//进厂日期
        @"selfAmount":self.loanDic[@"selfAmount"],//己方金额
        @"repairCompanyName":self.loanDic[@"repairCompanyName"],//修理厂
        @"repairCompanyId":self.repairCompanyId,//修理厂id
        @"chargeSide":self.loanDic[@"chargeSide"],//责任方
        @"compensateState":@(self.compensateState),//理赔状态
        @"outTime":[NSString stringRemoveWithNull:self.loanDic[@"outTime"]],//出厂日期
        @"pickState":@(self.pickState), //  提车状态
        @"repairState":@(self.repairState), //  修为状态
        @"thirdAmount":self.loanDic[@"thirdAmount"],//第三方金额
        @"description":self.loanDic[@"description"],//描述
        @"remark":self.loanDic[@"remark"], //  备注
        @"pics":self.annexDataAry, //  图片


        
    }.mutableCopy;
    
    NSString *url;
    // 新增保养
    if (self.type==1) {
        url = [NSString stringWithFormat:@"%@",POST_PostAddTinsuranceuse];
    }
    // 修改保养的时候需要穿id
    else if (self.type==2) {
        url = [NSString stringWithFormat:@"%@",POST_PostUpdateTinsuranceuse];
        [dic setValue:self.insuranceModel.ID forKey:@"id"];
    }

    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (self.type==1) {
                showAlert(@"新增出险成功");
            }
            else {
                showAlert(@"修改出险成功");
            }
           
            
            // carModel 有值说明从详情页进入,返回时须刷新列表
            if (!ObjectIsEmpty(self.carModel)) {
                kUserDefaultSetBool(YES, kRefreshStr);
            }
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    

}


-(void)uploadImage:(NSArray *)ary{
    
    [self.annexAry removeLastObject];
    MBProgressHUD *hud = [MBProgressHUD showGif];
        
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
    dispatch_async(queue, ^{
    
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        [MSNetworkManager postRevertUrl:POST_UploadFile_URL params:@{@"type":@"1"} formData:@[obj] success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,resonseObj[@"url"]];
                [self.annexAry addObject:@{
                    @"url":imageUrl
                }];
                [self.annexDataAry addObject:resonseObj[@"url"]];
                
                if (idx == ary.count-1) {
                    [hud hide];
                    [self.annexAry addObject:@{
                        @"url":@"upLoad_B",
                    }];
                   
                    [self.tableView reloadData];
                }
                dispatch_semaphore_signal(semaphore);
            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
            dispatch_semaphore_signal(semaphore);
            [hud hide];
        } withShowHud:NO];
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        
     }];
    });

}



@end
